<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="1100px"
    :visible.sync="visible">
    <el-form  style="max-height: 500px;overflow: auto;margin-top: 10px" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-row>
    <el-form-item label="模版名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="模版名称"></el-input>
    </el-form-item>
      <el-form-item label="平台类型" prop="platType" >
        <el-radio-group v-model="dataForm.platType" @change="hideSign">
          <el-radio :label="1">阿里云</el-radio>
          <el-radio :label="2">腾讯云</el-radio>
          <el-radio :label="3">七牛云</el-radio>
        </el-radio-group>
      </el-form-item>
        <el-form-item :label="AccessName" prop="platKey">
          <el-input v-model="dataForm.platKey" :placeholder="platKeyName"></el-input>
        </el-form-item>
        <el-col :span="24">
          <el-form-item :label="AccessKeyName" prop="platSecret">
            <el-input v-model="dataForm.platSecret" :placeholder="secretName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
    <el-form-item label="短信签名" prop="sign" v-if="isHideSign">
      <el-input v-model="dataForm.sign" placeholder="短信签名"></el-input>
    </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="短息模版编码" prop="code">
            <el-input v-model="dataForm.code" placeholder="短息模版编码"></el-input>
          </el-form-item>
        </el-col>



        <el-col :span="24">
        <el-form-item label="短信模版内容" prop="content">
          <el-input v-model="dataForm.content" type="textarea" placeholder="变量格式:${code}   示例:您的验证码为: ${code},该验证码5分钟内有效，请勿泄漏他人！！"
                    :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" resize="none" ></el-input>
        </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        AccessName:"AccessKeyId",
        AccessKeyName:"AccessKeySecret",
        isHideSign:true,
        visible: false,
        platKeyName:"阿里云AccessKeyId",
        secretName:"阿里云AccessKeySecret",
        dataForm: {
          id: 0,
          code: '',
          name: '',
          sign: '',
          content: '',
          platType: 1,
          platKey: '',
          platSecret: '',
          status: 1,
          replaceCode:''
        },
        dataRule: {
          code: [
            { required: true, message: '短息模版编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '模版名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '短信模版内容', trigger: 'blur' }
          ],
          platType: [
            { required: true, message: '请选择平台类型', trigger: 'blur' }
          ],
          platSecret: [
            { required: true, message: '请输入阿里云AccessKeySecret', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ],
          platKey: [
            { required: true, message: '请输入AccessKeyId', trigger: 'blur'},
          ]
        }
      }
    },
    methods: {
      aliyunAddOrUpdate(){
        this.platKeyName="阿里云AccessKeyId"
        this.AccessName="AccessKeyId"
        this.secretName="阿里云AccessKeySecret"
        this.AccessKeyName="AccessKeySecret",
          this.isHideSign=true
        this.dataRule={
          code: [
            { required: true, message: '短息模版编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '模版名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '短信模版内容', trigger: 'blur' }
          ],
          platType: [
            { required: true, message: '请选择平台类型', trigger: 'blur' }
          ],
          platSecret: [
            { required: true, message: '请输入阿里云AccessKeySecret', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ],
          platKey: [
            { required: true, message: '请输入阿里云AccessKeyId', trigger: 'blur'},
          ]
        }
      },
      tenXunAddOrUpdate(){
        this.platKeyName="腾讯云AppId"
        this.AccessName="AppId"
        this.isHideSign=true
        this.secretName="腾讯云AppKey"
        this.AccessKeyName="AppKey"
        this.dataRule={
          code: [
            { required: true, message: '短息模版编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '模版名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '短信模版内容', trigger: 'blur' }
          ],
          platType: [
            { required: true, message: '请选择平台类型', trigger: 'blur' }
          ],
          platSecret: [
            { required: true, message: '请输入腾讯云AppKey', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ],
          platKey: [
            { required: true, message: '请输入腾讯云AppId', trigger: 'blur'},
          ]
        }
      },
      qiNiuAddOrUpdate(){
        this.platKeyName="七牛云AccessKey"
        this.AccessName="AccessKey"
        this.secretName="七牛云SecretKey"
        this.AccessKeyName="SecretKey"
        this.isHideSign=false;
        this.dataRule={
          code: [
            { required: true, message: '短息模版编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '模版名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '短信模版内容', trigger: 'blur' }
          ],
          platType: [
            { required: true, message: '请选择平台类型', trigger: 'blur' }
          ],
          platSecret: [
            { required: true, message: '请输入七牛云SecretKey', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ],
          platKey: [
            { required: true, message: '请输入七牛云AccessKey', trigger: 'blur'},
          ]
        }
      },
      hideSign(value){
        if(value===1){
        this.aliyunAddOrUpdate();
        }else if(value===2){
         this.tenXunAddOrUpdate()
        }else if(value===3){
         this.qiNiuAddOrUpdate()
        }

      },
      init (id) {
        this.isHideSign=true
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`smsg/smsgmessagetemplate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.smsgMessageTemplate.code
                this.dataForm.name = data.smsgMessageTemplate.name
                this.dataForm.sign = data.smsgMessageTemplate.sign
                this.dataForm.content = data.smsgMessageTemplate.content
                this.dataForm.replaceCode = data.smsgMessageTemplate.replaceCode
                this.dataForm.platType = data.smsgMessageTemplate.platType
                this.dataForm.platKey = data.smsgMessageTemplate.platKey
                this.dataForm.platSecret = data.smsgMessageTemplate.platSecret
                this.dataForm.status = data.smsgMessageTemplate.status
                if(data.smsgMessageTemplate.platType===3){
                  this.qiNiuAddOrUpdate()
                }else if(data.smsgMessageTemplate.platType===2){
                  this.tenXunAddOrUpdate()
                }else if(data.smsgMessageTemplate.platType===1){
                  this.aliyunAddOrUpdate()
                }
              }
            })
          }else {
           this.aliyunAddOrUpdate()
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`smsg/smsgmessagetemplate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'sign': this.dataForm.sign,
                'content': this.dataForm.content,
                'replaceCode': this.dataForm.replaceCode,
                'platType': this.dataForm.platType,
                'platKey': this.dataForm.platKey,
                'platSecret': this.dataForm.platSecret,
                'status': this.dataForm.status,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
